#ifndef WU_THREAD_H
#define WU_THREAD_H
#include <mutex>
#include <condition_variable>
#include <thread>
#include <atomic>
#include <functional>
#include <memory>

class Thread;

struct Thread_notifyer
{
    std::function<void(int,int)> pstart;
    std::function<void(int,int)> pstop;
};

enum ThreadStatus
{
    uninit,
    initing,
    inited,
    runing,
    run,
    stoping,
    stopped
};

typedef std::shared_ptr<Thread_notifyer> ThreadNotifyer;
typedef std::function<void(std::shared_ptr<void> ptrArgs)> FuncType;

class Thread : public std::enable_shared_from_this<Thread>
{

private:
    const int                       id_;
    std::unique_ptr<std::thread>    pointer2stdthread_;
    std::atomic_int                 runflag_;
    FuncType                        customfunction_;
    std::shared_ptr<void>           customargs_;
    std::mutex                      signalmutex_;
    std::condition_variable         signalcond_;
    ThreadNotifyer                  pointer2control_;

public:
    Thread(const int id,ThreadNotifyer pNotify);
    int post(FuncType func, std::shared_ptr<void> args);
    int Init();
    int Uinit();
    int run();
    int stop();
    virtual ~Thread();
};

#endif
